package com.syj.jsxjj.ui.authentication_new.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.syj.jsxjj.events.ChangeTabMainEvent;
import com.syj.jsxjj.events.UIBaseEvent;
import com.syj.jsxjj.ui.authentication_new.fragment.OperatorFragment;
import com.syj.jsxjj.ui.authentication_new.fragment.PersonalInformationFragment;
import com.syj.jsxjj.ui.main.FragmentFactory;
import com.syj.jsxjj.widget.DrawableCenterTextView;
import com.syj.jsxjj.R;
import com.syj.jsxjj.app.App;
import com.syj.jsxjj.base.BaseActivity;
import com.syj.jsxjj.events.AuthenticationCenterEvent;
import com.syj.jsxjj.events.FragmentRefreshEvent;
import com.syj.jsxjj.ui.authentication_new.fragment.LinkmanFragment;
import com.syj.jsxjj.ui.authentication_new.fragment.SesameLetterFragment;
import com.syj.jsxjj.ui.main.WebViewActivity;
import com.tendcloud.tenddata.TCAgent;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by gyl on 2017/6/13 0013.
 * 认证步骤
 */

public class AuthenticationVerifityActivity extends BaseActivity {
    @BindView(R.id.tv_personal_information)
    TextView mTvPersonalInformation;
    @BindView(R.id.tv_linkman)
    TextView mTvLinkman;
    @BindView(R.id.tv_operator)
    TextView mTvOperator;
    @BindView(R.id.tv_sesame_letter)
    TextView mTvSesameLetter;
    @BindView(R.id.frame_fragment)
    FrameLayout mFrameFragment;
    @BindView(R.id.tv_close)
    DrawableCenterTextView mTvClose;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_right)
    DrawableCenterTextView mTvRight;
    private PersonalInformationFragment personalInformationFragment = new PersonalInformationFragment();//个人
    private LinkmanFragment linkmanFragment = new LinkmanFragment();//联系人
    private OperatorFragment operatorFragment = new OperatorFragment();//运营商
    private SesameLetterFragment sesameLetterFragment = new SesameLetterFragment();//芝麻
    private FragmentManager mSupportFragmentManager;
    private String mPhoneInfoUrl;
    private String mRiskCarditInfoUrl;
    private int mAuthStep;

    @Override
    public int getLayoutId() {
        return R.layout.activity_authentication_center;
    }

    @Override
    public void initPresenter() {
        EventBus.getDefault().register(this);
        initView();
    }

    public void initView() {
        mAuthStep = getIntent().getIntExtra("authStep", 0);
        Log.i("mAuthStep", mAuthStep + "");
        mRiskCarditInfoUrl = getIntent().getStringExtra("zmxyUrl");
        mPhoneInfoUrl = getIntent().getStringExtra("phoneUrl");
        Log.i("mRiskCarditInfoUrl", mRiskCarditInfoUrl + "");
    }

    @Override
    public void loadData() {
        setVisibleFragment(mAuthStep);
    }


    @OnClick({R.id.tv_close, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_close:
                finish();
                break;
            case R.id.tv_right:
                Intent intent = new Intent(AuthenticationVerifityActivity.this, WebViewActivity.class);
                if (getVisibleFragment().equals(personalInformationFragment)) {
                    MobclickAgent.onEvent(this, "Personal_solveQuestion");
                    TCAgent.onEvent(this, "Personal_solveQuestion");
                    intent.putExtra("url", App.getConfig().SOLVE_REGISTER);

                } else if (getVisibleFragment().equals(linkmanFragment)) {
                    MobclickAgent.onEvent(this, "Contact_solveQuestion");
                    TCAgent.onEvent(this, "Contact_solveQuestion");
                    intent.putExtra("url", App.getConfig().SOLVE_CONTACT);

                } else if (getVisibleFragment().equals(operatorFragment)) {
                    MobclickAgent.onEvent(this, "Phone_solveQuestion");
                    TCAgent.onEvent(this, "Phone_solveQuestion");
                    intent.putExtra("url", App.getConfig().SOLVE_PHONE);

                } else if (getVisibleFragment().equals(sesameLetterFragment)) {
                    MobclickAgent.onEvent(this, "ZhiMa_solveQuestion");
                    TCAgent.onEvent(this, "ZhiMa_solveQuestion");
                    intent.putExtra("url", App.getConfig().SOLVE_ZM);

                }
                startActivity(intent);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onVerifyStepEvent(AuthenticationCenterEvent event) {
        if (event.getMsg().equals("msg")) {
            setVisibleFragment(4);
        } else if (event.getMsg().equals("zmMesage")) {
            mTvSesameLetter.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.sesame_letter), null, null);
        } else if (event.getMsg().equals("phoneFinish")) {
            mTvOperator.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.operator), null, null);
            EventBus.getDefault().post(new FragmentRefreshEvent(UIBaseEvent.REFRESHINDEX));
        } else if (event.getMsg().equals("close")) {
            EventBus.getDefault().post(new FragmentRefreshEvent(UIBaseEvent.REFRESHINDEX));
            EventBus.getDefault().post(new ChangeTabMainEvent(FragmentFactory.FragmentStatus.Lend));
            finish();
        }
    }

    public Fragment getVisibleFragment() {
        FragmentManager fragmentManager = AuthenticationVerifityActivity.this.getSupportFragmentManager();
        List<Fragment> fragments = fragmentManager.getFragments();
        for (Fragment fragment : fragments) {
            if (fragment != null && fragment.isVisible())
                return fragment;
        }
        return null;
    }

    private void setVisibleFragment(int authStep) {
        mSupportFragmentManager = getSupportFragmentManager();
        replaceFragment(personalInformationFragment);
        if (authStep == 1) {
            mTvTitle.setText("实名认证");
            replaceFragment(personalInformationFragment);
            MobclickAgent.onEvent(this, "skimPersonalInfo");
            TCAgent.onEvent(this, "skimPersonalInfo");
            mTvPersonalInformation.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_personal_information), null, null);
            mTvLinkman.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_linkman), null, null);
            mTvOperator.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_operator), null, null);
            mTvSesameLetter.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_sesame_letter), null, null);
        } else if (authStep == 2) {
            mTvTitle.setText("联系人");
            replaceFragment(linkmanFragment);
            MobclickAgent.onEvent(this, "skimContacts");
            TCAgent.onEvent(this, "skimContacts");
            mTvPersonalInformation.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.personal_information), null, null);
            mTvLinkman.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_linkman), null, null);
            mTvOperator.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_operator), null, null);
            mTvSesameLetter.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_sesame_letter), null, null);
        } else if (authStep == 3) {
            mTvTitle.setText("运营商");
            replaceFragment(operatorFragment);
            MobclickAgent.onEvent(this, "operator");
            TCAgent.onEvent(this, "operator");

            Bundle bundle = new Bundle();
            bundle.putString("PhoneInfoUrl", mPhoneInfoUrl);
            operatorFragment.setArguments(bundle);

            mTvPersonalInformation.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.personal_information), null, null);
            mTvLinkman.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.linkman), null, null);
            mTvOperator.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_operator), null, null);
            mTvSesameLetter.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_sesame_letter), null, null);
        } else if (authStep == 4) {
            mTvTitle.setText("芝麻授信");
            replaceFragment(sesameLetterFragment);
            MobclickAgent.onEvent(this, "sesameLetter");
            TCAgent.onEvent(this, "sesameLetter");

            Bundle bundle = new Bundle();
            bundle.putString("RiskCarditInfoUrl", mRiskCarditInfoUrl);
            sesameLetterFragment.setArguments(bundle);

            mTvPersonalInformation.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.personal_information), null, null);
            mTvLinkman.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.linkman), null, null);
            mTvOperator.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.operator), null, null);
            mTvSesameLetter.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.un_sesame_letter), null, null);
        } else if (authStep == 5) {
            mTvTitle.setText("芝麻授信");
            MobclickAgent.onEvent(this, "sesameLetter");
            TCAgent.onEvent(this, "sesameLetter");

            Bundle bundle = new Bundle();
            bundle.putString("RiskCarditInfoUrl", mRiskCarditInfoUrl);
            sesameLetterFragment.setArguments(bundle);
            replaceFragment(sesameLetterFragment);

            mTvPersonalInformation.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.personal_information), null, null);
            mTvLinkman.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.linkman), null, null);
            mTvOperator.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.operator), null, null);
            mTvSesameLetter.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.sesame_letter), null, null);
        }

        OnButtonClick();
    }

    private void OnButtonClick() {
        personalInformationFragment.setOnButtonClick(new PersonalInformationFragment.OnButtonClick() {
            @Override
            public void onClick(View view) {
                setVisibleFragment(2);
            }
        });

        linkmanFragment.setOnButtonClick(new LinkmanFragment.OnButtonClick() {
            @Override
            public void onClick(View view) {
                setVisibleFragment(3);
            }
        });
    }

    private void replaceFragment(Fragment laterfragment) {
        mSupportFragmentManager.beginTransaction()
                .setCustomAnimations(R.anim.slide_right_in, R.anim.slide_left_out)
                .replace(R.id.frame_fragment, laterfragment)
                .commitAllowingStateLoss();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
